package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.zany.system.service.OrgInfoService;
import com.zany.tree.vo.OrgInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.action.SessionInfo;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JsonTools;

public class OrgInfoAction extends AbstractActionSupport{

	private static final long serialVersionUID = -5195852935546222622L;
	private static Log logger = LogFactory.getLog(OrgInfoAction.class);
	private OrgInfoService orgInfoService;
	private OrgInfoVO orgInfoVO = new OrgInfoVO();
	private List<OrgInfoVO> orgList = new ArrayList<OrgInfoVO>();
	
	public void getList() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("OrgInfoAction.getList()");}
		response = ServletActionContext.getResponse();
		String id = request.getParameter("node");
		if(id!=null&&!id.trim().equals("0")){
			orgInfoVO.setParentOrg(id.trim());
		}else{
			orgInfoVO.setParentOrg("/");
		}
		orgList = orgInfoService.getBelowOrg(orgInfoVO);
		String listNode = "[";
		for(OrgInfoVO orgVO:orgList){
			listNode += "{id:'"+orgVO.getOrgCode()+"',text:'"+orgVO.getOrgName()+"'"+(orgVO.getIsBottom().equals("1")?",leaf:true":"")+"},";
		}
		listNode = listNode.substring(0,listNode.length()-1) + "]";
		AjaxTools.exAjax(listNode,response);
	}
	
	public void getInfo() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("OrgInfoAction.getInfo()");}
		response = ServletActionContext.getResponse();
		orgInfoVO.setOrgCode(request.getParameter("orgCode"));
		orgInfoVO = orgInfoService.getOrg(orgInfoVO);
		orgList.add(orgInfoVO);
		AjaxTools.exAjax("{results:" + orgList.size() + ",ZANY:" + JsonTools.lstBeanToJson(orgList) + "}", response);
	}
	
	public void delete() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("OrgInfoAction.delete()");}
		response = ServletActionContext.getResponse();
		orgInfoVO.setOrgCode(request.getParameter("orgCode")); 
		try{
			orgInfoService.deleteOrg(orgInfoVO);
			AjaxTools.exAjax("{success:true}", response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	public void insert() throws Exception{
		if(logger.isInfoEnabled()){logger.info("OrgInfoAction.insert()");}
		response = ServletActionContext.getResponse();
		orgInfoVO = (OrgInfoVO)RequestBeanUtil.getReqToBean(OrgInfoVO.class,request);
		if(orgInfoVO.getParentOrg()==null){
			AjaxTools.exAjax("{\"success\":false,msg:\"请选择上级机构！\"}",response);
			return;
		}
		OrgInfoVO parentVO = new OrgInfoVO();
		parentVO.setOrgCode(orgInfoVO.getParentOrg());
		parentVO = orgInfoService.getOrg(parentVO);
		if(parentVO.getIsBottom().equals("1")){
			AjaxTools.exAjax("{\"success\":false,msg:\"上级机构不能为底层机构！\"}",response);
			return;
		}
		if(!parentVO.getIsVisible().equals("Y")){
			AjaxTools.exAjax("{\"success\":false,msg:\"上级机构需为可使用机构！\"}",response);
			return;
		}
		OrgInfoVO presentVO = new OrgInfoVO();
		presentVO = orgInfoService.getOrg(orgInfoVO);
		if(presentVO!=null){
			AjaxTools.exAjax("{\"success\":false,msg:\"此机构代码已存在！\"}",response);
			return;
		}
		parentVO = orgInfoService.getOrgSeq(orgInfoVO);
		//orgInfoVO.setOrgCode(String.valueOf(parentVO.getMaxCode()+1));
		orgInfoVO.setOrgLvl(String.valueOf((orgInfoVO.getParentOrg().toString().length()+1)/2+1));
		orgInfoVO.setSeq(parentVO.getMaxSeq()+1);
		try{
			orgInfoService.insertOrg(orgInfoVO);
			AjaxTools.exAjax("{success:true}", response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	
	public void update() throws Exception{
		if(logger.isInfoEnabled()){logger.info("OrgInfoAction.update()");}
		response = ServletActionContext.getResponse();
		orgInfoVO = (OrgInfoVO)RequestBeanUtil.getReqToBean(OrgInfoVO.class,request);
		if(orgInfoVO.getParentOrg()==null){
			AjaxTools.exAjax("{\"success\":false,msg:\"请选择所要修改机构！\"}",response);
			return;
		}
		orgInfoVO.setCreatedBy(SessionInfo.getUserCode(request));
		try{
			orgInfoService.updateOrg(orgInfoVO);
			AjaxTools.exAjax("{success:true}", response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
	}

	public OrgInfoService getOrgInfoService() {
		return orgInfoService;
	}

	public void setOrgInfoService(OrgInfoService orgInfoService) {
		this.orgInfoService = orgInfoService;
	}

}
